//
//  ZPMineDownloadViewController.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2016/12/4.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineDownloadViewController.h"
#import "ZPMineDownloadTabViewCell.h"
#import "ZPDownloadContents.h"
#import "ZPShareView.h"

@interface ZPMineDownloadViewController ()

@property (weak, nonatomic) IBOutlet UITableView *downloadTableView;
@property (weak, nonatomic) IBOutlet UIView      *downloadBottowView;
@property (weak, nonatomic) IBOutlet UILabel     *capacitySize;
@property (weak, nonatomic) IBOutlet UIView      *useCapacitySize;
@property (weak, nonatomic) IBOutlet UIView      *container;
@property (weak, nonatomic) IBOutlet UIButton    *deleteBtn;

@property (strong, nonatomic) UIButton           *leftBtn;
@property (strong, nonatomic) UIButton           *rightBtn;
@property (copy,   nonatomic) NSMutableArray     *contentsArr;
@property (copy,   nonatomic) NSMutableArray     *deleteArr;
@property (assign, nonatomic) NSInteger           deleteNum;
@property (copy,   nonatomic) NSArray            *indexPaths;
@property (copy,   nonatomic) NSMutableIndexSet  *indexSet;
@end

static NSString * const reuseIdentifier = @"ZPMineDownloadTabViewCell";

@implementation ZPMineDownloadViewController


- (NSMutableArray *)contentsArr { // 下载的全部数据
    if (_contentsArr == nil) {
        _contentsArr = [NSMutableArray array];
        for (int i = 0; i < 29; i++) {
            ZPDownloadContents *content = [[ZPDownloadContents alloc]init];
            [_contentsArr addObject:content];
        }
    }
    return _contentsArr;
}



- (NSMutableArray *)deleteArr {// 需要删除的数据
    if (_deleteArr == nil) {
        _deleteArr = [NSMutableArray array];
    }
    return _deleteArr;
}

- (NSArray *)indexPaths {
    if (_indexPaths == nil) {
        _indexPaths = [NSArray array];
    }
    return _indexPaths;
}

- (NSMutableIndexSet *)indexSet {
    if (_indexSet == nil) {
        _indexSet = [NSMutableIndexSet indexSet];
    }
    return _indexSet;
}



#pragma mark - <setupViewWithNib>
- (void)setupViewWithNib {
    ViewRadius(self.container, 4);
    self.downloadTableView.dataSource = self;
    self.downloadTableView.delegate   = self;
}


#pragma mark - <viewDidLoad>
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewWithNib];
    [self setupNavigationItem];
//    [self setupBottowBtn];
}



#pragma mark - <viewWillAppear>
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
}



#pragma mark - <viewDidAppear>
- (void)viewDidAppear:(BOOL)animated {
    
}



#pragma mark - <setupNavigationItem>
- (void)setupNavigationItem { // 设置导航栏上的子控件
    self.navigationItem.title = @"我的下载";
    [self.navigationItem setHidesBackButton:YES];
    
    // 导航左按钮
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.frame = CGRectMake(0, 0, 24, 24);
    [[ZPShareView sharedSingleView] setupButton:self.leftBtn backgroundColor:nil image:@"zhenpin_back" title:nil titltColor:nil titleFont:0];
    [self.leftBtn addTarget:self action:@selector(clickNavLeftBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftBtn];

    // 导航右按钮
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.frame = CGRectMake(0, 0, 50, 30);
    [[ZPShareView sharedSingleView] setupButton:self.rightBtn backgroundColor:clear_color image:nil title:@"编辑" titltColor:white_color titleFont:17.];
    [self.rightBtn addTarget:self action:@selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    
}



#pragma mark - <点击导航左侧按钮>
- (void)clickNavLeftBtn:(id)sender { // 点击导航栏左边的按钮
    
    if (self.downloadTableView.isEditing) { // 编辑下
        
        UIButton *leftBtn = (UIButton *)sender;
        
        leftBtn.selected = !leftBtn.selected;
        
        
        if (leftBtn.selected) { // 全选
            
            for (int i = 0; i < self.contentsArr.count; i++) { // 利用循环选中每一个元素
                
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.downloadTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
            }
            [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        }
        
        else { // 取消全选
            for (int i = 0; i < self.contentsArr.count; i++) { // 利用循环选中每一个元素
                
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.downloadTableView deselectRowAtIndexPath:indexPath animated:NO];
                
            }
            [leftBtn setTitle:@"全选" forState:UIControlStateNormal];
        }
        
    }
    else { // 非编辑返回上一级界面
        [self.navigationController popViewControllerAnimated:YES];
    }
}



#pragma mark - <点击导航右侧按钮>
- (void)clickEdit:(id)sender { //点击导航栏右侧按钮
    
    UIButton *rightBtn = (UIButton *)sender;
    rightBtn.selected = !rightBtn.selected;
    
    if (rightBtn.selected) {
        self.downloadBottowView.hidden = YES;
        [self.deleteBtn setHidden:NO];
        [self.deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
        
        self.leftBtn.xj_width = 50;
        [[ZPShareView sharedSingleView] setupButton:self.leftBtn backgroundColor:nil image:nil title:@"全选" titltColor:white_color titleFont:17.];
        self.leftBtn.selected = NO;
    }
    else {
        self.downloadBottowView.hidden = NO;
        [self.deleteBtn setHidden:YES];
        [rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
        self.leftBtn.frame = CGRectMake(0, 0, 24, 24);
        [[ZPShareView sharedSingleView] setupButton:self.leftBtn backgroundColor:nil image:@"zhenpin_back" title:nil titltColor:nil titleFont:0];
    }
    
     [self.downloadTableView setEditing:!self.downloadTableView.isEditing animated:YES]; // 设置表格表编辑状态
    
    if (self.downloadTableView.isEditing) { // 编辑状态
        
        self.downloadTableView.frame = CGRectMake(0, 0, Main_Screen_Width,Main_Screen_Height - 36); // 编辑状态下表格高度上调删,不被删除按钮遮挡
    }
    else { // 非编辑状态
        
        self.downloadTableView.frame = CGRectMake(0, 0, Main_Screen_Width,Main_Screen_Height);
        //根据偏移量计算是否滑动到tableView最后一行 count为无符号整型 强转一下
        if (self.downloadTableView.contentOffset.y > 44.0000000*(NSInteger)(self.contentsArr.count - 13 - 2)) {
            //cell高度*(模型数组个数 - 页面展示总cell数 + 2) = 滑动到tableView倒数第二行的偏移量
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.contentsArr indexOfObject:[self.contentsArr lastObject]] inSection:0];
            
            [self.downloadTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
    }
}



// 点击删除
- (void)delete {
    
    // 选中表格cell的集合
    self.indexPaths = self.downloadTableView.indexPathsForSelectedRows;

    // 确定选择的是哪些数据
    for (NSIndexPath *indexPath in  self.indexPaths ) {
       // 把数据源中选中的元素加入到需要删除的集合中
        [self.indexSet addIndex:indexPath.row];
    }
    self.deleteNum = self.indexPaths.count;
    ZPLog(@"要删除的个数-----%ld",self.deleteNum);
    
    if (self.indexPaths.count) {
        
        NSString *titleStr = [NSString stringWithFormat:@"确认删除所选的%ld个文件吗",self.indexPaths.count];
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles: nil];
        [actionSheet showInView:self.view];
    }else {
    }
}



#pragma mark - <UIActionSheetDelegate>
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        // 从总数据中删除被选中的数据
        [self.contentsArr removeObjectsAtIndexes:self.indexSet];
        [self.downloadTableView reloadData];
//        [self.downloadTableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationBottom];
    }
    if (buttonIndex == 1) {
        self.deleteNum = 0;
    }
    
}




#pragma mark - <UITableViewDatasource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.contentsArr count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    ZPMineDownloadTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPMineDownloadTabViewCell class]) owner:self options:nil] lastObject];
    }
    ZPDownloadContents *content = self.contentsArr[row];
    cell.downloadImgView.image    = Z_IMAGE(content.download_imageName);
    cell.downloadContentName.text = content.download_contentName;
    cell.downloadContentSize.text = content.download_contentSize;
    cell.downloadContentDate.text = content.download_contentDate;
    return cell;
}



#pragma mark - <侧滑删除>
// 侧滑删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) // 表格编辑样式为删除状态
    {
        [self.contentsArr removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; // 删除效果待动画
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) // 表格编辑样式为插入状态
    {
       
    }
    else if (editingStyle == UITableViewCellEditingStyleNone)   // 表格无编辑样式
    [self.downloadTableView reloadData];
    
}



#pragma mark - <侧滑出现多个按钮>
// 侧滑时出现多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"点击了关注");
        
        // 收回左滑出现的按钮(退出编辑模式)
        tableView.editing = NO;
    }];
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.contentsArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
    return @[action1, action0];
}



#pragma mark - <数据刷新操作>
//- (IBAction)multipleRemove {
//    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
//    
//    self.removeButton.hidden = !self.tableView.isEditing;
//}
//
//- (IBAction)remove {
//    // self.tableView.indexPathsForSelectedRows = [0, 1]
//    
//    // 获得需要删除的酒模型数据
//    NSMutableArray *deletedWineArray = [NSMutableArray array];
//    for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
//        [deletedWineArray addObject:self.wineArray[indexPath.row]];
//    }
//    
//    // 删除模型数据
//    [self.wineArray removeObjectsInArray:deletedWineArray];
//    
//    // 刷新
//    [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];
//}




#pragma mark - <数据刷新操作>
//- (IBAction)remove {
//    // 获得所有被打钩的模型
//    NSMutableArray *deletedWineArray = [NSMutableArray array];
//    for (FLWine *wine in self.wineArray) {
//        if (wine.isChecked) {
//            [deletedWineArray addObject:wine];
//        }
//    }
//    
//    // 删除所有被打钩的模型
//    [self.wineArray removeObjectsInArray:deletedWineArray];
//    
//    
//    // 刷新表格
//    [self.tableView reloadData];
//}




// 修改"delete"未"删除"
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
